package top.codestudy.uuid_apt_processor;


import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;


import java.util.HashMap;
import java.util.Map;

import javax.lang.model.element.Modifier;



public class ClassBuilder {

    private static Map<String,String> mUuidMap = new HashMap<>();

    public ClassBuilder(Map<String,String> uuidMap){
        mUuidMap = uuidMap;
    }

    public TypeSpec generateJavaCode(){
        String mClassName = "DataTranslate";
        return TypeSpec.classBuilder(mClassName)
                .addModifiers(Modifier.PUBLIC)
                .addMethod(generateMethods())
                .addField(generateField())
                .build();
    }


    private FieldSpec generateField() {

        FieldSpec.Builder builder = FieldSpec.builder(Map.class,"uuidMap",Modifier.PUBLIC,Modifier.STATIC);

        return builder.build();
    }

    private MethodSpec generateMethods() {

        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("init")
                .addModifiers(Modifier.PUBLIC)
                .addException(Exception.class)
                .returns(void.class);
        methodBuilder.addCode("uuidMap = new java.util.HashMap<String,com.jht.bletool2java.characteristic.TranslateData>();\n");

        for (Map.Entry<String,String> entry : mUuidMap.entrySet()) {
            String uuidName = entry.getKey();
            String className = entry.getValue();
            methodBuilder.addCode("uuidMap.put(\"" + uuidName + "\" , new " + className +"());\n");
        }

        return methodBuilder.build();
    }

    public String getPackageName(){
        return "com.jht.bletool2java.util";
    }
}
